home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr10 / 184_01.zip / RUNAMD.DOC < prev    next >
Text File  |  1993-06-13  |  12KB  |  212 lines

  1. .HE HARDWARE FLOATING POINT ROUTINES FOR C/80-----------Ted Carnevale
  2.      Wheε ╔ talkeΣ witΦ Wal⌠ Bilofsk∙ oµ thσ Softwarσ Toolwork≤ ì
  3. ove≥ ß yea≥ ago¼ hσ suggesteΣ tha⌠ ╔ senΣ thi≤ t∩ thσ ├ Usersº ì
  4. Group¼ s∩ herσ i⌠ is«  Additiona∞ detail≤ abou⌠ thσ desigε oµ ì
  5. thesσ routines¼ anΣ possiblσ extension≤ t∩ othe≥ 8080/Z8░ ├ ì
  6. implementations¼ arσ containeΣ iε m∙ articlσ iε thσ Nov/Deπ 198╡ ì
  7. issuσ oµ Micro/System≤ Journa∞ (pp.46-54).
  8.      Mos⌠ oµ thσ codσ iε thesσ file≤ wa≤ writteε b∙ me¼ anΣ ╔ ì
  9. releasσ i⌠ t∩ thσ publiπ domain¼ grantinτ permissioε fo≥ non-ì
  10. profi⌠ use«  Thσ smal∞ amoun⌠ oµ codσ iε thesσ file≤ tha⌠ come≤ ì
  11. directl∙ froφ thσ C/8░ floating-poin⌠ librar∙ i≤ copyrigh⌠ 198│ ì
  12. b∙ Walte≥ Bilofsky¼ anΣ useΣ b∙ permission.
  13.      Thesσ file≤ contaiε ß replacemen⌠ fo≥ par⌠ oµ thσ C/8░ ì
  14. (Softwarσ Toolworks¼ versioε 3.░ fo≥ 8080⌐ floatinτ poin⌠ librar∙ ì
  15. tha⌠ use≤ thσ AM─ 9511┴ o≥ it≤ Inte∞ twin¼ thσ 8231┴ (hencefortΦ ì
  16. referreΣ t∩ a≤ thσ FPP⌐ t∩ speeΣ u≡ floatinτ poin⌠ matΦ b∙ morσ ì
  17. thaε aε orde≥ oµ magnitude«  I⌠ take≤ 23╕ second≤ fo≥ C/8░ t∩ ruε ì
  18. Savage'≤ benchmarδ (Dr.Dobb'≤ Journa∞ vol.9┤ (Aug.1984⌐ pp.110-ì
  19. 114⌐ oε ß 6mH· 8085¼ bu⌠ ß 4mH· machinσ witΦ ß 2mH· FP╨ need≤ ì
  20. onl∙ 1╢ second≤ usinτ thesσ routines«  
  21.      MCHIP8░ b∙ Jame≤ Dicδ (CU╟ 162⌐ doe≤ mucΦ thσ samσ a≤ thσ ì
  22. patcΦ describeΣ here¼ bu⌠ therσ arσ somσ importan⌠ differences«  ì
  23. MCHIP8░ illustrate≤ ho≈ t∩ modif∙ thσ performancσ oµ softwarσ ì
  24. buil⌠ froφ ß librar∙ oµ linkablσ module≤ wheε al∞ tha⌠ i≤ knowε ì
  25. abou⌠ theφ i≤ thσ name≤ oµ thei≥ entr∙ points¼ ho≈ the∙ pas≤ ì
  26. parameter≤ anΣ results¼ anΣ wha⌠ function≤ the∙ accomplish«  ì
  27. Unfortunately¼ thσ differencσ betweeε thσ format≤ useΣ b∙ C/8░ ì
  28. anΣ thσ FP╨ t∩ represen⌠ floatinτ poin⌠ value≤ complicate≤ thi≤ ì
  29. approach«  I⌠ force≤ thσ use≥ oµ MCHIP8░ t∩ abandoε initializaì
  30. tioε oµ floats¼ anΣ require≤ specia∞ atof(⌐ anΣ ftoa(⌐ functions.
  31.      M∙ aiφ wa≤ t∩ squeezσ a≤ mucΦ speeΣ a≤ possiblσ ou⌠ oµ C/8░ ì
  32. whilσ hidinτ unnecessar∙ detail≤ froφ thσ user«  ╔ particularl∙ ì
  33. didn'⌠ wan⌠ t∩ bσ bothereΣ witΦ anythinτ tha⌠ woulΣ restric⌠ ì
  34. portabilit∙ oµ sourcσ code¼ sincσ ╔ collaboratσ witΦ othe≥ user≤ ì
  35. oµ C/8░ wh∩ d∩ no⌠ havσ thσ benefi⌠ oµ aε FPP«  Thσ mos⌠ direc⌠ ì
  36. wa∙ t∩ d∩ thi≤ wa≤ t∩ rewritσ par⌠ oµ C/80'≤ floatinτ poin⌠ ì
  37. library«  Fortunately¼ Softwarσ Toolwork≤ provide≤ thσ sourcσ ì
  38. code¼ s∩ thi≤ tasδ wa≤ ß relativel∙ straightforward.
  39.  
  40.  
  41.                          ABOUT THE PATCH
  42.  
  43.      Thi≤ patcΦ fo≥ C/80'≤ floatinτ poin⌠ librar∙ handle≤ al∞ oµ ì
  44. thσ dirt∙ worδ relateΣ t∩ conversioε betweeε thσ differen⌠ ì
  45. floatinτ poin⌠ representation≤ useΣ b∙ C/8░ anΣ thσ FP╨ (seσ ì
  46. note≤ beforσ labe∞ C2AMD║ iε FL3.MAC)«  C/8░ anΣ thσ FP╨ botΦ usσ ì
  47. ß 2┤ bi⌠ two'≤ complemen⌠ mantissß normalizeΣ t∩ ß valuσ betweeε ì
  48. 0.╡ anΣ 1«  However¼ C/8░ assume≤ tha⌠ thσ higΦ bi⌠ oµ thσ ì
  49. mantissß i≤ ▒ (reasonablσ sincσ it'≤ normalized)¼ anΣ use≤ thσ ì
  50. 24tΦ bi⌠ fo≥ mantissß sign¼ s∩ i⌠ caε represen⌠ number≤ froφ 2^-ì
  51. 12╕ t∩ 2^127«  Thσ 9511┴ o≥ 8231A¼ whicΦ retain≤ thσ higΦ bi⌠ oµ ì
  52. thσ mantissa¼ ha≤ t∩ pu⌠ thσ mantissß sigε bi⌠ iε thσ exponen⌠ ì
  53. byte¼ anΣ caε handlσ onl∙ 2^-6┤ t∩ 2^+63.
  54.      Thσ conversioε routine≤ arσ a⌠ label≤ C2AMD║ anΣ AMD2C║ iε ì
  55. FL3.MAC«  Thesσ routine≤ arσ alway≤ interposeΣ betweeε thσ res⌠ ì
  56. oµ C/8░ anΣ thσ FPP¼ s∩ thσ use≥ caε emplo∙ C/80'≤ floatinτ poin⌠ ìèinitializatioε anΣ thσ origina∞ atof(⌐ anΣ ftoa(⌐ function≤ ì
  57. withou⌠ concerε fo≥ thσ forma⌠ difference.
  58.      ╔ firs⌠ wrotσ thesσ routine≤ a≤ function≤ anΣ testeΣ theφ ì
  59. witΦ fpπ (filσ FPC.C)¼ ß prograφ tha⌠ print≤ thσ bi⌠ pattern≤ fo≥ ì
  60. variou≤ floatinτ poin⌠ value≤ beforσ anΣ afte≥ conversion«  Thi≤ ì
  61. prograφ ma∙ bσ helpfu∞ fo≥ user≤ witΦ othe≥ ├ implementation≤ ì
  62. whosσ inne≥ working≤ arσ no⌠ detaileΣ iε thσ manual«  Filσ ì
  63. C80DEF.╚ contain≤ definition≤ tha⌠ ╔ finΣ helpfu∞ wheε usinτ ì
  64. C/80.
  65.      Somσ oµ thσ routine≤ iε thi≤ patcΦ arσ concerneΣ witΦ ì
  66. passinτ argument≤ froφ C/8░ t∩ thσ FP╨ anΣ returninτ result≤ t∩ ì
  67. C/80«  Thσ brieµ descriptioε oµ paramete≥ anΣ resul⌠ passinτ iε ì
  68. thσ C/8░ manua∞ i≤ accompanieΣ b∙ ß suggestioε tha⌠ interesteΣ ì
  69. user≤ examinσ thσ codσ produceΣ b∙ thσ compile≥ t∩ figurσ i⌠ ou⌠ ì
  70. fo≥ themselves«  Thσ sourcσ codσ iε file≤ LGFLTLIB.AS═ anΣ ì
  71. FLOATLIB.AS═ tha⌠ comσ witΦ C/8░ wa≤ ver∙ helpfu∞ fo≥ decipherinτ ì
  72. wha⌠ happen≤ t∩ argument≤ anΣ functioε results.
  73.      Thσ typica∞ floatinτ poin⌠ operatioε require≤ morσ thaε 16╖ ì
  74. microsecond≤ (usec⌐ jus⌠ t∩ conver⌠ format≤ anΣ pas≤ datß bacδ ì
  75. anΣ fortΦ t∩ thσ FP╨ oε ß 4mH· machinσ (8080¼ 808╡ o≥ Z80)«  Thi≤ ì
  76. i≤ slo≈ enougΦ tha⌠ i⌠ doesn'⌠ seeφ wortΦ thσ effor⌠ t∩ replacσ ì
  77. thσ floating-poin⌠ additioε anΣ subtractioε routines«  ╔ als∩ ì
  78. didn'⌠ bothe≥ replacinτ floatinτ poin⌠ comparison¼ o≥ int-to-ì
  79. floa⌠ conversioε routines«  However¼ division¼ multiplication¼ ì
  80. anΣ thσ othe≥ function≤ arσ mucΦ faste≥ witΦ thσ hardwarσ matΦ ì
  81. chip.
  82.      Test≤ indicateΣ tha⌠ i⌠ woulΣ bσ beneficia∞ t∩ replacσ thσ ì
  83. softwarσ multiplicatioε anΣ divisioε routine≤ fo≥ long≤ witΦ ì
  84. hardwarσ operations«  C/8░ anΣ thσ FP╨ usσ thσ samσ four-bytσ ì
  85. forma⌠ fo≥ longs¼ s∩ thi≤ shoulΣ bσ easy«  An∙ volunteer≤ t∩ d∩ ì
  86. this?
  87.      Beforσ replacinτ C/80'≤ floatinτ poin⌠ operation≤ witΦ FP╨ ì
  88. routines¼ ╔ wrotσ ß se⌠ oµ function≤ tha⌠ usσ thσ FP╨ t∩ ì
  89. duplicatσ C/80'≤ floatinτ poin⌠ operations¼ anΣ assembleΣ theφ ì
  90. witΦ M80«  ╔ testeΣ theφ witΦ ß prograφ tha⌠ steppeΣ througΦ ß ì
  91. serie≤ oµ arguments¼ feedinτ theφ t∩ C80'≤ origina∞ floatinτ ì
  92. poin⌠ librar∙ anΣ t∩ m∙ specia∞ FP╨ routines¼ anΣ compareΣ thσ ì
  93. result≤ t∩ bσ surσ the∙ gavσ thσ samσ answer≤ a≤ thei≥ softwarσ ì
  94. counterparts«  WhicΦ the∙ did.
  95.      ╔ dissecteΣ FLIBRARY.RE╠ witΦ LI┬ usinτ thσ /╠ optioε t∩ ì
  96. determinσ tha⌠ thσ modulσ tha⌠ containeΣ thσ floatinτ poin⌠ ì
  97. multiplicatioε anΣ divisioε routine≤ wa≤ calleΣ FLTLIB«  Thσ ì
  98. sourcσ codσ filσ FLOATLIB.AS═ contain≤ thσ sourcσ fo≥ severa∞ ì
  99. modules¼ includinτ FLTLIB«  FLTLI┬ end≤ witΦ ß RE╘ jus⌠ beforσ ì
  100. thσ labe∞ f_stak:.
  101.  
  102.  
  103.                     HOW TO INSTALL THE PATCH
  104.  
  105.      Thσ file≤ FL1.MAC¼ FL2.MAC¼ anΣ FL3.MA├ arσ replacement≤ fo≥ ì
  106. portion≤ oµ thσ FLOATLIB.AS═ filσ provideΣ witΦ C/80«  Thesσ werσ ì
  107. writteε fo≥ Microsoft'≤ M80¼ bu⌠ the∙ caε bσ altereΣ easil∙ fo≥ ì
  108. usσ witΦ Digita∞ Research'≤ RMA├ o≥ thσ assemble≥ provideΣ witΦ ì
  109. C/80.
  110.      Usinτ you≥ favoritσ editor¼ extrac⌠ thσ FLTLI┬ codσ froφ ìèFLOATLIB«  AdΣ thσ lis⌠ oµ ENTRYs¼ EXTRNs¼ DSEG≤ etc« containeΣ ì
  111. iε FL1.MAC«  Modif∙ thσ fou≥ worΣ tablσ a⌠ Ftab║ a≤ indicateΣ b∙ ì
  112. FL2.MA├ (commen⌠ ou⌠ thσ thirΣ anΣ fourtΦ DWs)«  Theε cu⌠ ou⌠ thσ ì
  113. codσ froφ fmult3║ t∩ jus⌠ beforσ pophrt║ anΣ replacσ i⌠ witΦ thσ ì
  114. content≤ oµ FL3.MAC«  Bσ surσ t∩ changσ thσ i/∩ por⌠ addresse≤ t∩ ì
  115. sui⌠ you≥ particula≥ hardware!
  116.      ╔ havσ no⌠ includeΣ thσ absolutσ valuσ o≥ polynomia∞ ì
  117. function≤ (fabs(⌐ anΣ F_poly()⌐ iε thi≤ patch¼ althougΦ the∙ arσ ì
  118. par⌠ oµ thσ original MATHLIB«  Iµ yo⌡ wan⌠ t∩ includσ thesσ iε you≥ ì
  119. versioε oµ FLTL┬ (yes¼ ╔ nameΣ thσ patcheΣ modulσ FLTL┬ t∩ ì
  120. distinguisΦ i⌠ froφ thσ origina∞ FLTLIB)¼ usσ C/8░ t∩ generatσ ì
  121. thσ appropriatσ assemble≥ codσ froφ thσ ├ sourcσ anΣ patcΦ thσ ì
  122. assembl∙ codσ int∩ you≥ FLTL┬ beforσ assembly«  Iµ yo⌡ arσ reall∙ ì
  123. ambitious¼ yo⌡ coulΣ speeΣ u≡ Fpoly(⌐ eveε morσ b∙ usinτ ß ì
  124. modifieΣ "synthetiπ divisionó algorithφ tha⌠ leave≤ intermediatσ ì
  125. result≤ iε thσ FPP'≤ stack«  B∙ eliminatinτ repeateΣ unnecessar∙ ì
  126. conversion≤ oµ intermediatσ result≤ t∩ anΣ froφ C/80'≤ floa⌠ ì
  127. format¼ thi≤ shoulΣ acceleratσ Fpoly(⌐ considerably.
  128.      Whateve≥ you≥ choice¼ thσ nex⌠ ste≡ i≤ t∩ namσ thσ resultinτ ì
  129. filσ NUFLIB.MA├ anΣ assemblσ i⌠ witΦ M8░ t∩ producσ NUFLIB.REL«  ì
  130. No≈ usσ LI┬ t∩ sni≡ thσ FLTLI┬ modulσ ou⌠ oµ FLIBRARY.RE╠ tha⌠ ì
  131. camσ witΦ you≥ cop∙ oµ C/8░ anΣ replacσ i⌠ witΦ NUFLIB«  Thσ ì
  132. followinτ SUBMI╘ filσ shoulΣ d∩ thσ job:
  133.  
  134.           ;start of runlib.sub
  135.           XSUB
  136.           LIB
  137.           NEWLIB=FLIBRARY<..FLTLIB-1>
  138.           NUFLIB,FLIBRARY<FLTLIB+1..>
  139.           /E
  140.           ;end of runlib.sub
  141.  
  142. Thσ resul⌠ shoulΣ bσ NEWLIB.REL¼ whicΦ wil∞ usσ thσ AM─ 9511┴ o≥ ì
  143. Inte∞ 8231┴ fo≥ floatinτ poin⌠ operations«  Feedinτ NEWLI┬ ì
  144. insteaΣ oµ FLIBRAR┘ t∩ you≥ linkinτ loade≥ wil∞ producσ CO═ file≤ ì
  145. that use the FPP for floating point math.
  146.      Don'⌠ forge⌠ tha⌠ NEWLI┬ contain≤ thσ squarσ roo⌠ anΣ tranì
  147. scendenta∞ function≤ tha⌠ arσ par⌠ oµ thσ olΣ MATHLIB«  Includinτ ì
  148. botΦ NEWLI┬ anΣ MATHLI┬ iε command≤ t∩ you≥ linke≥ ma∙ producσ aε ì
  149. erro≥ messagσ indicatinτ duplicatioε oµ thesσ routines«
  150.      Iµ yo⌡ havσ alread∙ useΣ LI┬ t∩ builΣ ß singlσ librar∙ ou⌠ ì
  151. oµ al∞ oµ thσ RE╠ file≤ tha⌠ yo⌡ use¼ yo⌡ wil∞ havσ troublσ ì
  152. extractinτ MATHLI┬ froφ you≥ library«  LI┬ caε incorporatσ ì
  153. module≤ witΦ ╖ characte≥ name≤ int∩ ß library¼ bu⌠ sincσ i⌠ wil∞ ì
  154. onl∙ accep⌠ name≤ witΦ ß maximuφ oµ ╢ character≤ wheε yo⌡ tr∙ t∩ ì
  155. acces≤ individua∞ module≤ iε ß library¼ i⌠ won'⌠ bσ ablσ t∩ finΣ ì
  156. MATHLIB«  Iµ thi≤ happens¼ yo⌡ havσ tw∩ choices║  builΣ ß ne≈ ì
  157. librar∙ froφ scratch╗ o≥ usσ LI┬ witΦ it≤ /╠ optioε t∩ identif∙ ì
  158. thσ namσ oµ thσ las⌠ modulσ tha⌠ precede≤ MATHLI(B⌐ anΣ thσ namσ ì
  159. oµ thσ firs⌠ modulσ tha⌠ follow≤ it«  If¼ fo≥ thσ sakσ oµ ì
  160. argument¼ thesσ module≤ werσ calleΣ PRE╓ anΣ NEXT¼ anΣ you≥ biτ ├ ì
  161. librar∙ wa≤ nameΣ CLIB¼ theε 
  162.  
  163.           LIB XCLIB=CLIB<..PREV>,CLIB<NEXT..>/E
  164. èwill extract MATHLIB from CLIB, producing library XCLIB.
  165.  
  166.  
  167.                   ERROR-DETECTION AND HANDLING
  168.  
  169.      Thσ patcheΣ FLTL┬ ha≤ specia∞ error-detectioε anΣ handlinτ ì
  170. routines«  Tw∩ type≤ oµ error≤ ma∙ occur«  Aε attemp⌠ t∩ pas≤ aε ì
  171. argumen⌠ tha⌠ i≤ to∩ smal∞ o≥ to∩ biτ fo≥ thσ FP╨ (outsidσ thσ ì
  172. rangσ 2^-6┤ t∩ 2^63⌐ produce≤ ß softwarσ error«  Thσ FP╨ itselµ ì
  173. produce≤ ß hardwarσ erro≥ iµ i⌠ encounter≤ arithmetiπ overflow¼ ì
  174. underflow¼ dividσ b∙ zero¼ takinτ thσ loτ o≥ squarσ roo⌠ oµ ß ì
  175. negativσ number¼ o≥ aε argumen⌠ to∩ biτ fo≥ thσ exponentia∞ o≥ ì
  176. inversσ sinσ o≥ cosinσ functions«  A≤ FLTL┬ i≤ presentl∙ written¼ ì
  177. an∙ onσ oµ thesσ error≤ result≤ iε aε erro≥ messagσ anΣ thσ codσ ì
  178. wil∞ exi⌠ t∩ thσ operatinτ system.
  179.      A⌠ time≤ i⌠ ma∙ bσ ß bette≥ strateg∙ t∩ handlσ certaiε ì
  180. hardwarσ errors¼ sucΦ a≤ underflow¼ b∙ returninτ ß zer∩ o≥ somσ ì
  181. othe≥ valuσ insteaΣ oµ abortinτ thσ program«  Thereforσ ╔ ì
  182. includeΣ ß functioε fpmask(⌐ tha⌠ caε bσ useΣ t∩ se⌠ thσ erro≥ ì
  183. detectioε masδ unde≥ prograφ control«  Thσ defaul⌠ conditioε i≤ ì
  184. t∩ tes⌠ for¼ anΣ crasΦ iε casσ of¼ an∙ hardwarσ error«  Iµ an∙ oµ ì
  185. thσ FP╨ erro≥ test≤ i≤ disabled¼ i⌠ i≤ u≡ t∩ thσ programme≥ t∩ ì
  186. tes⌠ fo≥ anΣ handlσ sucΦ error≤ iε ß meaningfu∞ way«  Thi≤ migh⌠ ì
  187. bσ donσ witΦ ß ├ routinσ tha⌠ check≤ thσ statu≤ registe≥ ì
  188. immediatel∙ afte≥ an∙ suspec⌠ operation¼ branchinτ t∩ aε error-ì
  189. handlinτ routinσ iµ aε erro≥ occurs.
  190.  
  191.  
  192.                            CONCLUSION
  193.  
  194.      Thσ fina∞ resul⌠ oµ Savage'≤ benchmarδ usinτ thσ FP╨ i≤ 232╖ ì
  195. (roundeΣ ofµ t∩ thσ neares⌠ integer--erro≥ 2E+2)«  Thi≤ i≤ ì
  196. comparablσ t∩ singlσ precisioε FORTRAN¼ slightl∙ bette≥ accurac∙ ì
  197. (ß dubiou≤ terφ here⌐ thaε witΦ softwarσ arithmetic¼ anΣ jus⌠ a≤ ì
  198. gooΣ (o≥ bad⌐ a≤ an∙ othe≥ Am951▒ o≥ Inte∞ 8231┴ FP╨ iε thσ list«  ì
  199. I⌠ i≤ no⌠ sufficien⌠ fo≥ ill-conditioneΣ matri° equations¼ bu⌠ i⌠ ì
  200. i≤ morσ thaε adequatσ fo≥ les≤ demandinτ applications¼ sucΦ a≤ ì
  201. averaginτ anΣ displa∙ transformation≤ oµ datß followinτ A/─ ì
  202. conversion«  However¼ thσ speeΣ oµ operatioε i≤ faste≥ thaε an∙ ì
  203. othe≥ ╕ bi⌠ machinσ runninτ a⌠ 4mHz¼ anΣ abou⌠ 1╡ time≤ faste≥ ì
  204. thaε C80'≤ softwarσ floatinτ poin⌠ library«  
  205.      Keep on crunching!
  206.  
  207. Ted Carnevale
  208. Neurology Dept., SUNY
  209. HSC T12 Rm020
  210. Stony Brook, N.Y. 11794
  211. 12/27/85
  212.